package com.jingogo.service.impl;

import com.jingogo.common.BusinessException;
import com.jingogo.entity.Address;
import com.jingogo.mapper.AddressMapper;
import com.jingogo.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class AddressServiceImpl implements AddressService {
    @Autowired
    private AddressMapper addressMapper;

    @Override
    public void addAddress(Address address) {
        if (address.getUserId() == null || address.getReceiverName() == null) {
            throw new BusinessException("参数不完整");
        }
        if (Boolean.TRUE.equals(address.getIsDefault())) {
            addressMapper.updateAllNotDefault(address.getUserId());
        }
        addressMapper.insert(address);
    }

    @Override
    public void editAddress(Address address) {
        if (address.getId() == null) throw new BusinessException("地址ID不能为空");
        if (Boolean.TRUE.equals(address.getIsDefault())) {
            addressMapper.updateAllNotDefault(address.getUserId());
        }
        addressMapper.update(address);
    }

    @Override
    public void deleteAddress(Long addressId) {
        if (addressId == null) throw new BusinessException("地址ID不能为空");
        addressMapper.deleteById(addressId);
    }

    @Override
    public List<Address> listAddress(Long userId) {
        if (userId == null) throw new BusinessException("用户ID不能为空");
        return addressMapper.selectByUserId(userId);
    }
} 